(function ($) {
	$.resourceTopic = function () {
		return {	//书本基本信息
			pageSize: 5,
			pageIndex: 0,
			bookStatus: 0,
			pageParam: {},
			init: function(){
				this.listener();
				this.initLayui();
				this.resourceList();
			},
			/**
			 * 添加各按钮所需要的监听事件
			 */
			listener: function(){
                /**
                 *页面调整
                 * **/
                $.resourceTopic.resize();
                $(window).resize($.resourceTopic.resize);
				//点击单个引用
				$(".container").on("click",".topicImport",function(){
					var idList=[];
					idList.push($(this).data('id'));
					var submitData = {
						idList: idList,
						unitName: $(this).data('unitname'),
						token:User.token
					};
					$.resourceTopic.outTopicImport(submitData);
				});
				//点击选中引用
				$(".topicImportSelected").on("click",function(){
					var idList=[];
					var inputs=$(this).parents('#resourceTopic').find(".container input[type='checkbox']:checked");
					if(inputs.length>0){
						$.each(inputs,function(i,inputDom){
							idList.push($(inputDom).data('id'));
						});
						var submitData = {
							idList: idList,
							unitName: $(this).data('unitname'),
							token:User.token
						};
						$.resourceTopic.outTopicImport(submitData);
					}
				});
			},
            /**
             * 页面设置高度
             * **/
            resize: function () {
                $(".container").css("height", window.innerHeight - 115 + "px");
            },
			outTopicImport:function(submitData){
				//引用完成后input全置为空
				$("input[type='checkbox']").attr("checked",false);
				$.each($("input[type='checkbox']"),function(i,inputs){
					$(inputs).attr("checked",false);
					$.resourceTopic.initLayui();
				});
				updateObject(Doman.url+Doman.outTopicImport, true, JSON.stringify(submitData), function (result) {
					if(result.code=="200"){
						layer.msg(result.message, {icon: 1});
					}else{
						layer.msg(result.message, {icon: 2});
					}
				}, function () {
                });
			},
			initLayui: function(){
				layui.use(['layer','form','laypage','table'], function(){
					var $ = layui.jquery, layer = layui.layer, form = layui.form,table = layui.table, laypage = layui.laypage;
					form.render('checkbox');
					//全选
					form.on('checkbox(allChoose)', function(data){
						var child = $(data.elem).parents('#resourceTopic').find('.container input[type="checkbox"]');
						child.each(function(index, item){
							item.checked = data.elem.checked;
						});
						form.render('checkbox');
					});
            	});
			},
			resourceList: function(ifPage){
                if (!ifPage) {
                    $.resourceTopic.pageIndex = 0;
                }
				var submitData = {
					unitid: getParam("unitid"),
                    page: $.resourceTopic.pageIndex,
                    size:$.resourceTopic.pageSize
				};
                updateObject(Doman.url+Doman.outTopicList, true, JSON.stringify(submitData), function (result) {
					$(".totalNum span").html(result.topicDTOPage.totalElements);
					//设置title
					$(".title").html(result.courseName+'-'+result.unitName+'-'+'课后作业列表');
					//全选加上unitname
					$(".topicImportSelected").attr("data-unitname",result.unitName);
					//题目详情
					if(result.topicDTOPage.content.length>0){
						var html='';
						$.each(result.topicDTOPage.content,function(i,data){
							html+='<div class="item">'
								+ '    <div class="head">'
								+ '        <div>第'+($.resourceTopic.pageSize*$.resourceTopic.pageIndex+i+1)+'题</div>'
								+ '        <div>题型：'+data.type+'</div>'
								+ '        <div>'+data.author+'</div>'
								+ '        <div>'+data.isSubjective+'</div>'
								+ '        <div class="control">'
								+ '            <input type="checkbox" name="" lay-skin="primary" data-id='+data.id+'>'
								+ '            <a class="topicImport" data-unitname='+result.unitName+' data-id='+data.id+'>引用</a>'
								+ '        </div>'
								+ '     </div>'
								+ '    <div class="body">'+data.content
								+ '    </div>'
								+ ' </div>'
						});
						$(".container").html(html);
						$.resourceTopic.initLayui();
						$('.mathquill-editable:not(.mathquill-rendered-math)').mathquill('editable');
						$('.mathquill-textbox:not(.mathquill-rendered-math)').mathquill('textbox');
						$('.mathquill-embedded-latex').mathquill();
					}
					$.resourceTopic.initPage(result.topicDTOPage.totalElements);
               }, function () {
                });
			},
			/**
			 * 初始化分页页码
			 * @returns
			 */
			initPage: function(total){
				layui.use(['laypage'], function(){
					$ = layui.jquery, layer = layui.layer, laypage = layui.laypage;
					laypage.render({
						elem: 'pagination'
						,count: total //数据总数，从服务端得到
						,first: '首页'
						,last: '尾页'
						,layout: [ 'prev', 'page', 'next', 'skip','limit']
						,curr: $.resourceTopic.pageIndex+1
						,limit: $.resourceTopic.pageSize
						,limits:[5,10,15,20]
						,skip: true
						,theme: '#337ab7'
						,jump: function(obj, first){
							$.resourceTopic.pageIndex = obj.curr-1;
							$.resourceTopic.pageSize = obj.limit;
							//首次不执行
							if(!first){
								$.resourceTopic.resourceList(true);
							}
						}
					});
				});
			}
		}
	}();
    $.resourceTopic.init();
})(jQuery);